unit FieldDefinition;

interface

uses
  OmniXML;

type
  TFieldDefinition = class
  private
    fName: String;
    fType: String;
    fTableName: String;
    fIsPrimaryKey: boolean;
    fIsForeignKey: boolean;
    fForeignKeyTable: String;
    fForeignKeyName: String;
  public
    constructor Create(aNode: IXMLNode; aTableName: String);
    destructor Destroy(); override;
    procedure setName(aVal: String);
    function getName(): String;
    procedure setTableName(aVal: String);
    function getTableName(): String;
    procedure setType(aVal: String);
    function getType(): String;
    procedure setIsPrimaryKey(aVal: boolean);
    function isPrimaryKey(): boolean;
    procedure setIsForeignKey(aVal: boolean);
    function isForeignKey(): boolean;
    procedure setForeignKeyTable(aVal: String);
    function getForeignKeyTable(): String;
    procedure setForeignKeyName(aVal: String);
    function getForeignKeyName(): String;
  end;

  TFieldDefinitions = array of TFieldDefinition;

implementation

uses
  SysUtils;

{ TFieldDefinition }

constructor TFieldDefinition.Create(aNode: IXMLNode; aTableName: String);
begin
  inherited Create();
  if (aNode.SelectSingleNode('name') = nil) then
  begin
    raise Exception.Create('No name defined for field');
  end;
  setName(Trim(aNode.SelectSingleNode('name').Text));
  if (aNode.SelectSingleNode('type') = nil) then
  begin
    raise Exception.Create('No type defined for field');
  end;
  setType(Trim(aNode.SelectSingleNode('type').Text));
  if (aNode.SelectSingleNode('primaryKey') <> nil) then
  begin
    setIsPrimaryKey((Trim(aNode.SelectSingleNode('primaryKey').Text) = 'true'));
  end;
  if (aNode.SelectSingleNode('foreignKey') <> nil) then
  begin
    setForeignKeyName(Trim(aNode.SelectSingleNode('foreignKey').Text));
  end;
  if (aNode.SelectSingleNode('foreignTable') <> nil) then
  begin
    setForeignKeyTable(Trim(aNode.SelectSingleNode('foreignTable').Text));
  end;
  if (((aNode.SelectSingleNode('foreignKey') <> nil) AND (aNode.SelectSingleNode('foreignTable') = nil)) OR
    ((aNode.SelectSingleNode('foreignTable') <> nil) AND (aNode.SelectSingleNode('foreignKey') = nil))) then
  begin
    raise Exception.Create('Incomplete foreign table definition in field ' + getName());
  end;
  setIsForeignKey((aNode.SelectSingleNode('foreignKey') <> nil) AND (aNode.SelectSingleNode('foreignTable') <> nil));
  setTableName(Trim(aTableName));
end;


destructor TFieldDefinition.Destroy;
begin

  inherited;
end;

function TFieldDefinition.getForeignKeyName: String;
begin
  Result := fForeignKeyName;
end;

function TFieldDefinition.getForeignKeyTable: String;
begin
  Result := fForeignKeyTable;
end;

function TFieldDefinition.getName: String;
begin
  Result := fName;
end;

function TFieldDefinition.getTableName: String;
begin
  Result := fTableName;
end;

function TFieldDefinition.getType: String;
begin
  Result := fType;
end;

function TFieldDefinition.isForeignKey: boolean;
begin
  Result := fIsForeignKey;
end;

function TFieldDefinition.isPrimaryKey: boolean;
begin
  Result := fIsPrimaryKey;
end;

procedure TFieldDefinition.setForeignKeyName(aVal: String);
begin
  fForeignKeyName := aVal;
end;

procedure TFieldDefinition.setForeignKeyTable(aVal: String);
begin
  fForeignKeyTable := aVal;
end;

procedure TFieldDefinition.setIsForeignKey(aVal: boolean);
begin
  fIsForeignKey := aVal;
end;

procedure TFieldDefinition.setIsPrimaryKey(aVal: boolean);
begin
  fIsPrimaryKey := aVal;
end;

procedure TFieldDefinition.setName(aVal: String);
begin
  fName := aVal;
end;

procedure TFieldDefinition.setTableName(aVal: String);
begin
  fTableName := aVal;
end;

procedure TFieldDefinition.setType(aVal: String);
begin
  fType := aVal;
end;

end.
